<?php
/**
 * Plugins handler
 *
 * Actions:
 * 		Enable: enables the specified plugin.
 * 		Disable: disables the specified plugin.
 *		Upload: uploads a plugin to the server 
 *		Default: displays a list of all the available plugins, indicating which of the are enabled or not.
 */
require_once dirname(dirname(__FILE__)) .'/cse-init.php';

// authentication is required!
Cinnamon::registry('session')->applyAuthenticationPolicy(true);

// load classes
Cinnamon::loadClass('Container');
Cinnamon::loadClass('DataGrid');
Cinnamon::loadClass('ArrayCollection');

// fetch required objects from Cinnamon registry
$params = Cinnamon::registry('params');
$view = Cinnamon::registry('view');
$plugins = Cinnamon::registry('plugins');

// handle the requested action
switch ( $params->action ) {
	case 'enable':
		$plugins->enable($params->plugin_id);
		header('Location: plugins.php?enabled=true');
		break;
	case 'disable':
		$plugins->disable($params->plugin_id);
		header('Location: plugins.php?disabled=true');
		break;
	case 'upload':
		
		break;
	default:
		// assign messages
		if ( $params->enabled == 'true' ) {
			$view->addMessage(View::NOTICE, 'Plugin has been enabled successfully.');
		} else if ( $params->enabled == 'false' ) {
				$view->addMessage(View::ERROR, 'Plugin failed to be enabled.');
		}
		
		if ( $params->disabled == 'true' ) {
			$view->addMessage(View::NOTICE, 'Plugin has been disabled successfully.');
		} else if ( $params->disabled == 'false' ) {
			$view->addMessage(View::ERROR, 'Plugin failed to be disabled.');
		}		
	
		// render output
		$allPlugins = $plugins->getAllPlugins();
		
		if ( count($allPlugins) <=0 ){
			$view->addMessage(View::HELP, 'There aren&rsquo;t any available plugins for use. If you don&lsquo;t know what plugins are, here some useful plugins to get started: <ul><li>What are Plugins</li><li>Browse the available Plugins</li><li>How to Install a Plugin</li></ul>');
			$view->renderHeader();
		} else {
			$view->renderHeader();
			// create datagrid to be rendered
			$container = new Container('Installed plugins');
			$dp = new ArrayCollection($allPlugins);
			$dg = new DataGrid('Available plugins');
			$dg->addColumn('Plugins', 'function', 'pluginDescription');
			$dg->addColumn('Actions', 'function', 'pluginActions', 'width', 100);
			$dg->setDataProvider($dp);
			$container->addChild($dg);
		}
	
		// render output
		if ( count($allPlugins) > 0 ) {
			$container->render();
		}
		$view->renderFooter();	
		break;
}

//
// Functions
//
function pluginDescription($item) {
	return '<strong>'. $item->title .'</strong> '. $item->version .'<br />'. nl2br($item->description);
}
function pluginActions($item) {
	$value = '';
	if ( $item->isEnabled ) {
		$value .= '<button onclick="location.href=\'plugins.php?action=disable&amp;plugin_id='. $item->id .'\';">Disable</button>';
	} else {
		$value .= '<button onclick="location.href=\'plugins.php?action=enable&amp;plugin_id='. $item->id .'\';">Enable</button>';
	}
	return $value;
}
?>
